package com.linsen.core.ohc.config;

import com.linsen.core.common.annotation.AutoConfiguration;
import com.linsen.core.ohc.aspect.OHCachePutAspect;
import com.linsen.core.ohc.aspect.OHCacheRemoveAspect;
import com.linsen.core.ohc.aspect.OHCacheableAspect;
import com.linsen.core.ohc.serializer.FastJsonCacheSerializer;
import com.linsen.core.ohc.serializer.StringCacheSerializer;
import com.linsen.core.ohc.util.OHCacheUtil;
import org.caffinitas.ohc.Eviction;
import org.caffinitas.ohc.OHCache;
import org.caffinitas.ohc.OHCacheBuilder;
import org.springframework.context.annotation.Bean;

/**
 * @author zhaoyong
 * @date 2023/4/11
 */
@AutoConfiguration
public class LinsenOhcAutoConfiguration {
    @Bean
    public OHCache<String, Object> ohCache() {
        return OHCacheBuilder.<String, Object>newBuilder()
                .keySerializer(StringCacheSerializer.INSTANCE)
                .valueSerializer(FastJsonCacheSerializer.INSTANCE)
                .timeouts(Boolean.TRUE)
                .eviction(Eviction.LRU)
                .build();
    }

    @Bean
    public <V> OHCacheUtil<V> ohCacheUtil(OHCache<String, V> ohCache) {
        return new OHCacheUtil<>(ohCache);
    }

    @Bean
    public OHCacheableAspect ohCacheableAspect(OHCacheUtil<Object> ohCacheUtil) {
        return new OHCacheableAspect(ohCacheUtil);
    }

    @Bean
    public OHCachePutAspect ohCachePutAspect(OHCacheUtil<Object> ohCacheUtil) {
        return new OHCachePutAspect(ohCacheUtil);
    }

    @Bean
    public OHCacheRemoveAspect ohCacheRemoveAspect(OHCacheUtil<Object> ohCacheUtil) {
        return new OHCacheRemoveAspect(ohCacheUtil);
    }
}
